package com.api.customization.unionpay.market.web;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.customization.unionpay.market.service.ClearingRefundService;
import com.customization.unionpay.market.service.impl.ClearingRefundServiceImpl;
import com.customization.unionpay.market.util.FormDataUtils;
import com.engine.common.util.ServiceUtil;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

/**
 * @author sjm
 * @description 清算退款流程（营销退款）相关
 */
@Path("/market/clear/refund")
public class ClearingRefundController {

    private static final Logger log = LoggerFactory.getLogger(ClearingRefundController.class);

    private ClearingRefundService getService(User user) {
        return ServiceUtil.getService(ClearingRefundServiceImpl.class, user);
    }

    // 发起清算退款流程
    @POST
    @Path("/createWorkFlow")
    @Produces(MediaType.APPLICATION_JSON)
    public String externalGetInfo(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        JSONObject result = new JSONObject();
        try {
            result.put("msg", "查询成功");
            result.put("data", null);
            result.put("code", 200);
            result.put("errorCode", 1);
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);

            String body = FormDataUtils.readBodyUTF8(request);

            log.info("请求体参数：" + body);
            if (body != null) {
                JSONObject parseJson = JSONObject.parseObject(body);
                // 流水号
                String serialCode = parseJson.getString("SERIAL_CODE");
                // 申请人（工号或其他唯一标识）
                String applicant = parseJson.getString("APPLICANT");
                // 营销平台活动授权码
                String actAuthCode = parseJson.getString("ACT_AUTH_CODE");
                // 结余金额
                String balanceAmount = parseJson.getString("BALANCE_AMOUNT");
                // 附件
                String fileUrl = parseJson.getString("FILE_URL");
                // 资金核对一致
                String consistentFundVft = parseJson.getString("CONSISTENT_FUND_VFT");
                // 打款账户唯一
                String accountIsUnique = parseJson.getString("ACCOUNT_IS_UNIQUE");
                // M1=M2
                String mIsSame = parseJson.getString("M_IS_SAME");
                // 资金收付是否一致
                String fundIncomeIsSame = parseJson.getString("FUND_INCOME_IS_SAME");
                // 资金收入（配置）是否一致
                String fundIncomeConfigIsSame = parseJson.getString("FUND_INCOME_CONFIG_IS_SAME");
                // 出资金额与流水金额一致
                String investmentAmountIsSame = parseJson.getString("INVESTMENT_AMOUNT_IS_SAME");

                String source = parseJson.getString("SOURCE");
                String time = parseJson.getString("TIME");

                if (StrUtil.isBlank(serialCode)) {
                    result.put("msg", "流水号为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(applicant)) {
                    result.put("msg", "申请人为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(actAuthCode)) {
                    result.put("msg", "授权码为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(balanceAmount)) {
                    result.put("msg", "结余金额为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(consistentFundVft)) {
                    result.put("msg", "资金核对一致为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(accountIsUnique)) {
                    result.put("msg", "打款账户唯一为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(fundIncomeIsSame)) {
                    result.put("msg", "资金收付是否一致为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(fundIncomeConfigIsSame)) {
                    result.put("msg", "资金收入（配置）是否一致为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(investmentAmountIsSame)) {
                    result.put("msg", "出资金额与流水金额一致为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(source)) {
                    result.put("msg", "来源系统为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(time)) {
                    result.put("msg", "请求时间为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                JSONObject resultData = getService(user).clearRefundCreateWorkFlow(serialCode, applicant, actAuthCode, balanceAmount, fileUrl, consistentFundVft, accountIsUnique,mIsSame,fundIncomeIsSame,fundIncomeConfigIsSame,investmentAmountIsSame, source);
                result.put("data", resultData);
            } else {
                result.put("msg", "参数异常");
                result.put("data", null);
                result.put("code", 500);
                result.put("errorCode", 10003);
            }
        } catch (Exception e) {
            log.info("接口异常：" + e.getMessage());
            result.put("msg", "接口异常，请联系管理员！");
            result.put("data", null);
            result.put("code", 500);
            result.put("errorCode", 5000);
        }
        return JSONObject.toJSONString(result);
    }

}
